const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

module.exports = function() {
    var router=express.Router();
    //添加
    router.use('/Addforumbinfo',function (req,res,next) {
        var time=new Date();
        time=time.toLocaleString();
      if(req.files.length==0)
      {
        var picimg='time.jpg';
        db.query('INSERT INTO `forumbinfo`(`forumBId`, `forumBName`, `userId`, `forumBContent`, ' +
          '`forumBNum`, `forumBTime`, `forumBUp`,`forumbPic`, `forumAId`) VALUES ' +
          '(0,"'+req.body.fourmBName+'",'+parseInt(req.body.userName)+',"'+req.body.editor+'",0,"'+time+'",0,'+picimg+','+ +req.body.forumAName+')',function (err,data) {
          if(err){
            res.send('<script>alert("程序错误mmmmmmmmmm");window.location.href="400.html";</script>').end();
          }else{
            if(data.length==0){
              res.send('<script>alert("添加失败");window.location.href="400.html";</script>').end();
            }
            else{
              res.send('<script>alert("添加成功");window.location.href="forumbinfo.html";</script>').end();
            }
          }
        });
      }else {
        //console.log("原名文件"+req.files[0].originalname);
        //获得文件扩展名
        //console.log("文件后缀"+pathLib.parse(req.files[0].originalname).ext);
        //文件全名
        var ext = pathLib.parse(req.files[0].originalname).ext;
        //console.log("dxcvbnm" + ext);
        //文件旧存放路径
        var oldPath = req.files[0].path;
        // console.log("文件旧存放路径"+oldPath)
        //文件新存放路径
        var newPath = req.files[0].path + ext;
        // console.log("文件新存放路径" + newPath)
        //文件新名
        var newFileName = req.files[0].filename + ext;

        console.log("我上的图片" + newFileName);
        fs.rename(oldPath, newPath, function (err) {
          if (err) {
            res.status(500).send('upload is error');
          }
        db.query('INSERT INTO `forumbinfo`(`forumBId`, `forumBName`, `userId`, `forumBContent`, ' +
          '`forumBNum`, `forumBTime`, `forumBUp`,`forumbPic` ,`forumAId`) VALUES ' +
          '(0,"'+req.body.fourmBName+'",'+parseInt(req.body.userName)+',"'+req.body.editor+'",0,"'+time+'",0,"'+newFileName+'",'+parseInt(req.body.forumAName)+')',function (err,data) {
          if(err){
            console.log(err);
            res.send('<script>alert("程序错误sss");window.location.href="400.html";</script>').end();
          }else{
            if(data.length==0){
              res.send('<script>alert("添加失败");window.location.href="400.html";</script>').end();
            }
            else{
              res.send('<script>alert("添加成功");window.location.href="forumbinfo.html";</script>').end();
            }
          }
        });
        });
      }

    });

    //查询
    router.use('/Addforumbinfo.html',function (req,res,next) {
        db.query('SELECT * FROM forumainfo',function (err,data) {
            if(err){
                res.send('<script>alert("程序错误");window.location.href="400.html";</script>')
            }else{
                if(data.length==0) {
                    res.send('<script>alert("暂无您所需的数据");window.location.href="500.html";</script>');
                }
                else{
                    req.forumA=data;
                }
            }

        });
        
        db.query('select *from userinfo',function (err,data) {
            if(err){
                res.send('<script>alert("程序错误");window.location.href="400.html";</script>')
            }else{
                if(data.length==0) {
                    res.send('<script>alert("暂无您所需的数据");window.location.href="500.html";</script>');
                }
                else{
                    res.render('forum/Addforumbinfo.ejs',{formA:req.forumA,user:data})
                }
            }
        });

    });
    return router;
}
